<?php
class EMAIL {
// TODO: Add function to remove $to, $from, $cc & $bcc;
// TODO: Stop header injection at sender name
// TODO: Add function to add attachment 
// TODO: Add advance attachment control (Remote attachment, Remote file size)

	private $to;
	private $from;
	private $subject;
	private $body;
	private $cc;
	private $bcc;
	private $senderName;
	private $replyTo;
	private $xMailer;
		
	public function addTo($to) {
		// get the last index of $this->to
		$count = count($this->to);
	
		// If $to is an array
		if(is_array($to)) {
			// Check if all are correct email address
			if(!filter_var_array($to, FILTER_VALIDATE_EMAIL)) {
				return -1;
			}
			//add all to $this->to;
			foreach($to as $key=>$val) {
				$this->to[$count++] = $val; // adding and incrementing count
			}
		} else { // If $to is single email
			if(!filter_var($to, FILTER_VALIDATE_EMAIL)) {
				return -2;
			}
			// Add it to $this->to;
			$this->to[$count] = $to;
		}
		return 0;
	}
	public function addFrom($from) {
		// Validate $from
		if(!filter_var($from, FILTER_VALIDATE_EMAIL)) {
			return -1;
		}
		// add to $this->from
		$this->from = $from;
		return 0;
	}
	public function addSubject($sub, $type=1) { // $type = 1: Add/Replace   $type =2: Append
		if($type == 1) {			// Add/Replace
			$this->subject = $sub;
		} else if($type == 2) {
			if($this->subject != NULL) {  	// If not null append
				$this->subject .= $sub;
			} else {  			// If NULL add
				$this->subject = $sub;
			}
		} else {
			return -1;
		}
		return 0;
	}
	public function addBody($body, $type=1) { // $type = 1: Add/Replace   $type =2: Append
		if($type == 1) {			// Add/Replace
			$this->body = $body;
		} else if($type == 2) {
			if($this->body != NULL) {  	// If not null append
				$this->body .= $body;
			} else {  			// If NULL add
				$this->body = $body;
			}
		} else {
			return -1;
		}
		return 0;
	}
	public function addCc($cc) {
		// get the last index of $this->cc
		$count = count($this->cc);
	
		// If $to is an array
		if(is_array($cc)) {
			// Check if all are correct email address
			if(!filter_var_array($cc, FILTER_VALIDATE_EMAIL)) {
				return -1;
			}
			//add all to $this->cc;
			foreach($cc as $key=>$val) {
				$this->cc[$count++] = $val; // adding and incrementing count
			}
		} else { // If $cc is single email
			if(!filter_var($cc, FILTER_VALIDATE_EMAIL)) {
				return -2;
			}
			// Add it to $this->cc;
			$this->cc[$count] = $cc;
		}
		return 0;
	}
	
	public function addBcc($bcc) {
		// get the last index of $this->bcc
		$count = count($this->bcc);
	
		// If $to is an array
		if(is_array($bcc)) {
			// Check if all are correct email address
			if(!filter_var_array($bcc, FILTER_VALIDATE_EMAIL)) {
				return -1;
			}
			//add all to $this->bcc;
			foreach($bcc as $key=>$val) {
				$this->bcc[$count++] = $val; // adding and incrementing count
			}
		} else { // If $bcc is single email
			if(!filter_var($bcc, FILTER_VALIDATE_EMAIL)) {
				return -2;
			}
			// Add it to $this->bcc;
			$this->bcc[$count] = $bcc;
		}
		return 0;
	}
	public function addSenderName($name, $type = 1) {  // $type: 1-Add/Replace 2-Append
		if(!(is_string($name) || is_null($name))) {
			return -1;	
		}
		if($type == 1) {
			$this->senderName = $name;
		} else {
			if($this->senderName == NULL) {
				$this->senderName = $name;
			}else {
				$this->senderName .= $name;
			}
		}
		return 0;
	}
	public function addReplyTo($reply) {
		// Validate $reply
		if(!filter_var($reply, FILTER_VALIDATE_EMAIL)) {
			return -1;
		}
		// add to $this->replyTo
		$this->replyTo = $reply;
		return 0;
	}
	public function setXmailer($xMailer = 1) {  // 1- attach mailer    0/NULL-Do not add mailer
		if($xMailer == 1 || $xMailer == 0 || $xMailer == NULL) {
			$this->xMailer = $xMailer;
			return 0;
		} else {
			return -1;
		}
	}
	public function send() {
		// Check presence of essential components
		if(!is_array($this->to)) {
			return -1;
		}
		if($this->subject == NULL) {
			return -2;
		}
		if($this->body == NULL) {
			return -3;
		}
	
		// Prepare All the coponents
		
		// Prepare $to
		$to = "";
		$count = 0;
		foreach($this->to as $key=>$val) {
			if($count == 0) {
				$to = $val;
				$count++;
			} else {
				$to .= ", $val";
			}
		}
		
		// Prepare $cc
		// Check if $cc is present
		$cc = NULL;
		if(is_array($this->cc)) {
			$cc = "";
			$count = 0;
			foreach($this->cc as $key=>$val) {
				if($count == 0) {
					$cc = $val;
					$count++;
				} else {
					$cc .= ", $val";
				}
			}
		}

		// Prepare $bcc
		// Check if $bcc is present
		$bcc = NULL;
		if(is_array($this->bcc)) {
			$bcc = "";
			$count = 0;
			foreach($this->bcc as $key=>$val) {
				if($count == 0) {
					$bcc = $val;
					$count++;
				} else {
					$bcc .= ", $val";
				}
			}
		}
		
		// Prepare header
		$header = "";
		
		// Check if From is present
		if($this->from) {
			$header .= "From: ". $this->senderName ."<" . $this->from . ">\r\n";
		}
		
		// Check if $cc is present;
		if($cc !== NULL) {
			$header .= "Cc: " . $cc . "\r\n";
		}
		// Check if $cc is present;
		if($bcc !== NULL) {
			$header .= "Bcc: " . $bcc . "\r\n";
		}
		// Check if reply to is present
		if($this->replyTo != NULL) {
			$header .= "Reply-To: " . $this->replyTo . "\r\n";
		}
		// Check if xmailer is present
		if($this->xMailer == 1) {
			$header .= "X-Mailer: PHP/" . phpversion() . "\r\n";
		}
		

		// Send the mail
		mail($to, $this->subject, $this->body, $header);
		return 0;
	}	
}

?>